<%@page contentType="text/html;charset=UTF-8" %>
<%@page pageEncoding="UTF-8" %>
<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title><spring:message code="html.title"/></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="<c:url value="/static/styles/style.css" />" type="text/css"/>
    <link type="text/css" href="<c:url value="/static/styles/overcast/jquery-ui-1.8.17.custom.css" />"
          rel="Stylesheet"/>
    <script type="text/javascript" src="<c:url value="/static/js/jquery-1.7.1.min.js" />"></script>
    <script type="text/javascript" src="<c:url value="/static/js/jquery-ui-1.8.17.custom.min.js" />"></script>
    <script type="text/javascript">
        $.ajaxSetup({ cache:false });
    </script>
    <script type="text/javascript" src="<c:url value="/static/js/jquery.validate.js" />"></script>
</head>
<body>
<div id="locationPage">
    <%-- Menu --%>
    <c:import url="../include/menu.jsp">
        <c:param name="page" value="users"/>
    </c:import>

    <%-- Location data --%>
    <c:choose>
        <c:when test="${user.id < 0}">
            <h2><spring:message code="header.users.add"/></h2>
            <spring:url var="url" value="/users/add"/>
        </c:when>
        <c:otherwise>
            <h2><spring:message code="header.users.edit"/></h2>
            <spring:url var="url" value="/users/update"/>
        </c:otherwise>
    </c:choose>

    <form:form action="${url}" method="POST" commandName="user" id="edit-user-form">
        <div class="params">
            <form:hidden path="id"/>
            <div class="row">
                <div class="title"><spring:message code="field.users.create.date"/></div>
                <div class="value">
                    <form:input path="createDate" type="text"/>
                </div>
            </div>
            <div class="row">
                <div class="title"><spring:message code="field.users.expires.date"/></div>
                <div class="value">
                    <form:input path="expireDate" type="text"/>
                </div>
            </div>
            <div class="row">
                <div class="title"><spring:message code="field.users.name"/></div>
                <div class="value">
                    <form:input path="userName"/>
                </div>
                <form:errors path="userName" cssClass="validationError" element="label" title="userName"/>
            </div>
            <c:if test="${error=='name'}">
                <div class="validationError"><spring:message code="msg.users.duplicate.name"/></div>
            </c:if>
            <div class="row">
                <div class="title"><spring:message code="field.users.license.type"/></div>
                <div class="value"><form:select path="licenseType" items="${licensesMap}"/></div>
            </div>
            <div class="row">
                <div class="title"><spring:message code="field.users.license.code"/></div>
                <div class="value"><form:input path="licenseCode" disabled="true"/></div>
                <form:hidden path="licenseCode"/>
            </div>
            <div class="row">
                <div class="title"><spring:message code="field.users.mac.address"/></div>
                <c:choose>
                    <c:when test="${user.mac!=null}">
                        <div class="value"><form:input path="mac" disabled="true"/></div>
                    </c:when>
                    <c:otherwise>
                        <div class="value"><spring:message code="marker.mac.not.set"/></div>
                    </c:otherwise>
                </c:choose>
                <form:hidden path="mac"/>
            </div>
            <div class="row">
                <div class="title"><spring:message code="field.users.mac.validation"/></div>
                <div class="value"><form:checkbox path="macChecking"/></div>
            </div>
        </div>
        <div class="do">
            <input type="submit" value="<spring:message code="action.users.save"/>"/>
            <input type="button" value="<spring:message code="action.users.cancel"/>"
                   onclick="document.location.href='<c:url value="/users/view"/>'"/>
            <c:choose>
                <c:when test="${user.mac!=null}">
                    <input type="button" value="<spring:message code="actions.users.reset.mac"/>"
                           onclick="document.location.href='<c:url value="/users/resetMac/${user.id}"/>'"/>
                </c:when>
            </c:choose>
        </div>
    </form:form>
</div>

<script type="text/javascript">
    var validateForm = $("form#edit-user-form").validate({
        rules:{
            userName:{
                required:true,
                minlength:5,
                maxlength:20,
                remote:"<c:url value="/users/isUnique/?userId=${user.id}"/>"
            },
            licenseCode:{
                required:true,
                maxlength:255,
                minlength:5
            }
        },
        messages:{
            userName:{
                remote:"<spring:message code="msg.users.duplicate.name"/>"
            }
        }
    });

    $(function () {
        $.datepicker.setDefaults($.datepicker.regional[""]);
        $("#createDate").datepicker($.datepicker.regional["" ]);
        $("#expireDate").datepicker($.datepicker.regional["" ]);
//        $("#createDate").datepicker({ dateFormat: 'yy-mm-dd' });
//        $("#expireDate").datepicker({ dateFormat: 'yy-mm-dd' });
    });

    $(document).ready(function () {
        validateForm.form();
    });
</script>

</body>
</html>